package com.itheima.service.mongo;

import com.itheima.domain.db.User;
import com.itheima.domain.mongo.Sound;
import com.itheima.domain.mongo.UserSound;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class SoundServiceImpl implements SoundService {
    @Autowired
    MongoTemplate mongoTemplate;

    //接受录音
    @Override
    public Sound getSounds(Long userId,String gender)

    {
        List<Sound>soundList=new ArrayList<>();
        Sound sound=new Sound();
        if(gender.equals("man"))
        {
            Query query=new Query(
                    Criteria.where("gender").is("women")
            );
         soundList=mongoTemplate.find(query,Sound.class);
            int size = (soundList.size())-1;
            sound=soundList.get(size);

            System.out.println("获取的这条记录的内容"+sound.toString());
            ObjectId id=sound.getId();
            Query query1 = new Query(
                    Criteria.where("id").is(id)
            );
                //得到语音以后就删除了
                mongoTemplate.remove(query1,Sound.class);

             //将次数减一（userSound）,并且更新
            Integer time=findTimeById(userId);
            time--;
            Query query2=new Query(
                    Criteria.where("userId").is(userId)
            );
            Update update=new Update();
            update.set("remaingTimes",time);
            mongoTemplate.updateFirst(query2,update,UserSound.class);




        }else
            {
            Query query = new Query(
                    Criteria.where("gender").is("man")
            );
            //优化方案，如果没有语音了该怎么办？？查不到语音给前台一个如何的提示？
            soundList = mongoTemplate.find(query, Sound.class);

            //优化方案，写个随机数，随机 获取里面的语音条
                int size = (soundList.size())-1;
                sound=soundList.get(size);
                System.err.println("获取的这条记录的内容"+sound.toString());
                ObjectId id=sound.getId();
                Query query1 = new Query(
                        Criteria.where("id").is(id)
                );

                //将声音从表（sound）中移除
                mongoTemplate.remove(query1, Sound.class);

                //将次数减一（userSound）,并且更新
                Integer time=findTimeById(userId);
                time--;
                Query query2=new Query(
                        Criteria.where("userId").is(userId)
                );
                Update update=new Update();
                update.set("remaingTimes",time);
                mongoTemplate.updateFirst(query2,update,UserSound.class);




        }
        return sound;
    }

    //存录音

    @Override
    public void save(Sound sound1)
    {
        mongoTemplate.save(sound1);

    }

    //查询所有录音
    @Override
    public List<UserSound> findAll() {
        return mongoTemplate.findAll(UserSound.class);
    }

    //根据用户id查询使用次数
    @Override
    public Integer findTimeById(Long userId)
    {

        Query query=new Query(
                Criteria.where("userId").is(userId)
        );

       List<UserSound>userSoundList=mongoTemplate.find(query,UserSound.class);
       UserSound userSound=userSoundList.get(0);
      Integer time=userSound.getRemaingTimes();
        return time;
    }

    @Override
    public void savaUserSoound(UserSound userSound) {

        mongoTemplate.save(userSound);
    }
}
